package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.domain.Address;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.AddressService;
import com.itheima.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.UUID;

@RequestMapping("/address")
@RestController
public class AddressController {

    @Reference
    private AddressService addressService;

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/upload")
    public Result uploadFile(@RequestParam("imgFile") MultipartFile imgFile){

        Result result = null;
        Jedis jedis = null;

        try{

            //获取上传文件的后缀
            String originalFilename = imgFile.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));

            //创建存储的文件名
            String key = UUID.randomUUID().toString().replace("-" , "") + extension;

            QiniuUtils.upload2Qiniu(imgFile.getBytes() , key);

            //文件上传成功 ， 进行 redis 存储
            jedis = jedisPool.getResource();

            jedis.sadd(RedisConstant.IMAGE_URL_ADDRESS_QINIU , key);

            result = new Result(true  , MessageConstant.PIC_UPLOAD_SUCCESS , key);

        }catch (Exception ex){
            ex.printStackTrace();
            result = new Result(false , MessageConstant.PIC_UPLOAD_FAIL);

        }

        if (jedis != null) {
            jedis.close();
        }

        return  result;
    }

    @RequestMapping("/findWithPage")
    public PageResult findWithPage(@RequestBody QueryPageBean bean){

        PageResult pageResult = addressService.findWithPage(bean);

        System.out.println(pageResult.getRows());


        return pageResult;


    }

    @RequestMapping("/addAddress")
    public Result addAddress(@RequestBody Address address){

        Result result = null;

        try{
            addressService.addAddress(address);

            result = new Result(true , MessageConstant.ADD_ADDRESS_SUCCESS);


        }catch (Exception ex){

            ex.printStackTrace();

            result = new Result(false , MessageConstant.ADD_ADDRESS_FAIL);
        }


        return result;

    }

    @RequestMapping("/deleteAddress")
    public Result deleteAddress(int addressId){

        Result result = null;

        try{

            addressService.deleteAddress(addressId);

            result = new Result(true , MessageConstant.DELETE_ADDRESS_SUCCESS);


        }catch (Exception ex){

            ex.printStackTrace();

            result = new Result(false , MessageConstant.DELETE_ADDRESS_FAIL);
        }


        return result;



    }

    @RequestMapping("/prepareUpdate")
    public Result prepareUpdate(int addressId){
        Result result = null;

        try{

            Address address = addressService.findById(addressId);

            result = new Result(true , MessageConstant.QUERY_ADDRESS_SUCCESS , address);

        }catch (Exception ex){

            ex.printStackTrace();

            result = new Result(false , MessageConstant.QUERY_ADDRESS_FAIL);
        }


        return result;






    }


    @RequestMapping("/updateAddress")
    public Result updateAddress(String hasOldImg , @RequestBody Address address){


        Result result = null;

        try{

            addressService.updateAddress(hasOldImg , address);

            result = new Result(true , MessageConstant.EDIT_ADDRESS_SUCCESS);

        }catch (Exception ex){

            ex.printStackTrace();

            result = new Result(false , MessageConstant.EDIT_ADDRESS_FAIL);
        }


        return result;


    }


    @RequestMapping("/findAll")
    public Result findAll(){

        Result result = null;

        try{

            List<Address> list = addressService.findAll();

            result = new Result(true , MessageConstant.ADD_ADDRESS_SUCCESS , list);


        }catch (Exception ex){

            ex.printStackTrace();

            result = new Result(false , MessageConstant.QUERY_ADDRESS_FAIL);



        }

        return  result;


    }




}
